package com.potenza.training.resume.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.potenza.training.resume.dao.*;
import com.potenza.training.resume.entity.*;
import com.potenza.training.resume.service.ResumeBasicService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.common.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;


@Service("resumeBasicService")
public class ResumeBasicServiceImpl extends ServiceImpl<ResumeBasicDao, ResumeBasicEntity> implements ResumeBasicService {

    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    ResumeBasicDao resumeBasicDao;
    @Autowired
    AdvantageDao advantageDao;
    @Autowired
    WorkExperienceDao workExperienceDao;
    @Autowired
    ResumeHopeDao resumeHopeDao;
    @Autowired
    EducationExperienceDao educationExperienceDao;
    @Autowired
    ItemExperienceDao itemExperienceDao;
    @Autowired
    ImageDao imageDao;
    @Autowired
    SocialDao socialDao;
    @Autowired
    ExamineDao examineDao;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<ResumeBasicEntity> page = this.page(
                new Query<ResumeBasicEntity>().getPage(params),
                new QueryWrapper<ResumeBasicEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public R updatePhone(String phone,String code) {
        HashOperations operations = redisTemplate.opsForHash();
        String pc = operations.get("phone", phone)+"";
        System.out.println(phone+"------"+code+"--------"+"--------"+pc);
        if(code.equals(pc)){
            return R.ok();
        }
        else{
            return R.error("验证码错误");
        }
    }

    @Override
    public ResumeBasicEntity findResumeByUid(Long uid) {
        QueryWrapper<ResumeBasicEntity> resumeBasicEntityQueryWrapper = new QueryWrapper<>();
        resumeBasicEntityQueryWrapper.lambda().eq(ResumeBasicEntity::getUid,uid);

        return resumeBasicDao.selectOne(resumeBasicEntityQueryWrapper);
    }

    @Override
    public List<ResumeBasicEntity> findResumeByClazzId(Integer clazzId) {
        QueryWrapper<ResumeBasicEntity> resumeBasicEntityQueryWrapper = new QueryWrapper<>();
        resumeBasicEntityQueryWrapper.lambda().eq(ResumeBasicEntity::getClazzId,clazzId);

        return resumeBasicDao.selectList(resumeBasicEntityQueryWrapper);
    }

    @Override
    public ResumeBasicEntity findResumeBasic(int resumeId) {
        QueryWrapper<ResumeBasicEntity> resumeBasicEntityQueryWrapper = new QueryWrapper<>();
        resumeBasicEntityQueryWrapper.lambda().eq(ResumeBasicEntity::getId,resumeId);
        return resumeBasicDao.selectOne(resumeBasicEntityQueryWrapper);
    }

    @Override
    public List<AdvantageEntity> findAdvange(int resumeId) {
        QueryWrapper<AdvantageEntity> advantageEntityQueryWrapper = new QueryWrapper<>();
        advantageEntityQueryWrapper.lambda().eq(AdvantageEntity::getResumeId,resumeId);

        return advantageDao.selectList(advantageEntityQueryWrapper);
    }

    @Override
    public List<WorkExperienceEntity> findWorkExpersoin(int resumeId) {
        QueryWrapper<WorkExperienceEntity> workExperienceEntityQueryWrapper = new QueryWrapper<>();
        workExperienceEntityQueryWrapper.lambda().eq(WorkExperienceEntity::getResumeId,resumeId);

        return workExperienceDao.selectList(workExperienceEntityQueryWrapper);
    }

    @Override
    public List<ResumeHope> findResumeHope(int resumeId) {
        QueryWrapper<ResumeHope> resumeHopeQueryWrapper = new QueryWrapper<>();
        resumeHopeQueryWrapper.lambda().eq(ResumeHope::getResumeId,resumeId);

        return resumeHopeDao.selectList(resumeHopeQueryWrapper);
    }

    @Override
    public List<EducationExperienceEntity> findEducation(int resumeId) {
        QueryWrapper<EducationExperienceEntity> educationExperienceEntityQueryWrapper = new QueryWrapper<>();
        educationExperienceEntityQueryWrapper.lambda().eq(EducationExperienceEntity::getResumeId,resumeId);


        return educationExperienceDao.selectList(educationExperienceEntityQueryWrapper);
    }

    @Override
    public List<ItemExperienceEntity> findItem(int resumeId) {
        QueryWrapper<ItemExperienceEntity> itemExperienceEntityQueryWrapper = new QueryWrapper<>();
        itemExperienceEntityQueryWrapper.lambda().eq(ItemExperienceEntity::getResumeId,resumeId);

        return itemExperienceDao.selectList(itemExperienceEntityQueryWrapper);
    }

    @Override
    public List<SocialEntity> findSocial(int resumeId) {
        QueryWrapper<SocialEntity> socialEntityQueryWrapper = new QueryWrapper<>();
        socialEntityQueryWrapper.lambda().eq(SocialEntity::getResumeId,resumeId);


        return socialDao.selectList(socialEntityQueryWrapper);
    }

    @Override
    public List<ImageEntity> findImage(int resumeId) {
        QueryWrapper<ImageEntity> imageDaoQueryWrapper = new QueryWrapper<>();
        imageDaoQueryWrapper.lambda().eq(ImageEntity::getResumeId,resumeId);

        return imageDao.selectList(imageDaoQueryWrapper);
    }

    @Override
    public R updateStatus(Integer id,String status,String reason,String image) throws Exception {
        //先修改主表状态
        ResumeBasicEntity entity = resumeBasicDao.selectById(id);
        entity.setStatus(Integer.parseInt(status));
        resumeBasicDao.updateById(entity);
        //新建一个主表状态进行添加
        ExamineEntity examineEntity = new ExamineEntity();
        //获取当前时间
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String format = simpleDateFormat.format(new Date());
        Date date = simpleDateFormat.parse(format);
        examineEntity.setDate(date);
        if(reason.equals("null")){
            examineEntity.setReason("恭喜!个人简历已被通过审核！！");
        }else{
            examineEntity.setReason(reason);
        }

        examineEntity.setImages(image);

        examineEntity.setResult(status);
        examineEntity.setResumeId(id);
        examineDao.insert(examineEntity);
        return R.ok();
    }

    @Override
    public ExamineEntity findExamine(int resumeId) {
        QueryWrapper<ResumeBasicEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(ResumeBasicEntity::getId,resumeId);
        ResumeBasicEntity resumeBasicEntity = resumeBasicDao.selectOne(queryWrapper);
        QueryWrapper<ExamineEntity> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(ExamineEntity::getResumeId,resumeBasicEntity.getId());
        return examineDao.selectOne(wrapper);
    }

}